package kuonta.web.forms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.UISelectItem;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;

import kuonta.domain.Congregacao;
import kuonta.domain.ItemRecibo;
import kuonta.domain.Recibo;
import kuonta.domain.TipoItemRecibo;
import kuonta.domain.TipoRecibo;
import kuonta.service.ServiceRecibo;

import org.ajax4jsf.component.UIAjaxCommandLink;
import org.ajax4jsf.component.html.HtmlAjaxCommandLink;
import org.ajax4jsf.component.html.HtmlAjaxRegion;
import org.apache.myfaces.custom.selectitems.UISelectItems;
import org.cerberus.core.application.ApplicationException;
import org.cerberus.web.jsf.FacesUtil;
import org.cerberus.web.jsf.ManagedBean;
import org.cerberus.web.jsf.annotations.MB;

@MB(bean = Recibo.class, facade = "serviceRecibo")
public class FormRecibo extends ManagedBean<Recibo, Long> {

	private static final long serialVersionUID = -8181637923741436748L;

	@SuppressWarnings("unused")
	private List<TipoRecibo> tiposRecibos;

	private HtmlPanelGrid panelGridItenRecibo = new HtmlPanelGrid();

	private List<TipoItemRecibo> tipoItens;
	
	private Integer tipoItemId = 0;
		
	public Integer getTipoItemId() {
		return tipoItemId;
	}

	public void setTipoItemId(Integer tipoItemId) {
		this.tipoItemId = tipoItemId;
	}

	public List<TipoItemRecibo> getTipoItens() {
		return tipoItens;
	}

	public void setTipoItens(List<TipoItemRecibo> tipoItens) {
		this.tipoItens = tipoItens;
	}

	public void setTiposRecibos(List<TipoRecibo> tiposRecibos) {
		this.tiposRecibos = tiposRecibos;
	}

	public List<TipoRecibo> getTiposRecibos() {
		TipoRecibo[] tipos = TipoRecibo.values();
		return Arrays.asList(tipos);
	}

	@Override
	public void save(ActionEvent e) {
		Congregacao congregacao = new Congregacao();// (Congregacao)FacesUtil.
		// getSessionAttribute
		// ("congregacao");
		congregacao.setId(1);

		List<ItemRecibo> itens = new ArrayList<ItemRecibo>();
		List<ItemRecibo> itensForm = getBean().getItens();

		for (ItemRecibo item : getBean().getItens()) {
			if (item.getValor() != null) {
				item.setRecibo(getBean());
				item.setData(getBean().getData());
				itens.add(item);
			}
		}

		getBean().setItens(itens);

		try {
			if (getBean().getItens().size() >= 1) {
				((ServiceRecibo) getFacade()).saveReciboWithFolhaItens(
						getBean(), congregacao.getId());
			}
		} catch (ApplicationException ae) {
			getBean().setItens(itensForm);
			FacesUtil.addErroMessage(ae.getMessage());
		}

		setBean(new Recibo());

	}

	@SuppressWarnings("unchecked")
	public void addNewComponent(ActionEvent ae) {
		List children = panelGridItenRecibo.getChildren();

		HtmlSelectOneMenu selectOne = new HtmlSelectOneMenu();
		selectOne.setLabel("Tipo do Item");
		selectOne.setRequired(true);
		selectOne.setValueExpression("", FacesUtil.getFacesContext()
				.getApplication().getExpressionFactory().createValueExpression(
						FacesUtil.getFacesContext().getELContext(),
						"#{formRecibo.bean.itens[0].tipo}", Integer.class));

		UISelectItem item1 = new UISelectItem();
		item1.setItemLabel("Selecione o Tipo do Item");
		selectOne.getChildren().add(item1);

		UISelectItems itens = new UISelectItems();

		FormTipoItem formTipoItem = new FormTipoItem();
		itens.setValue(formTipoItem.getList());
		itens.setVar("tipo");

		ValueExpression uiItemLabel = FacesUtil.getFacesContext()
				.getApplication().getExpressionFactory().createValueExpression(
						FacesUtil.getFacesContext().getELContext(),
						"#{tipo.descricao}", String.class);
		itens.setValueExpression("itemLabel", uiItemLabel);

		ValueExpression uiItemValue = FacesUtil.getFacesContext()
				.getApplication().getExpressionFactory().createValueExpression(
						FacesUtil.getFacesContext().getELContext(), "#{tipo}",
						String.class);
		itens.setValueExpression("itemValue", uiItemValue);

		selectOne.getChildren().add(itens);

		HtmlOutputText outPut = new HtmlOutputText();
		outPut.setStyle("padding-left:30px;");
		outPut.setValue("$");
		
		HtmlInputText inputText = new HtmlInputText();

		HtmlAjaxRegion regionAdd = new HtmlAjaxRegion();

		HtmlAjaxCommandLink commandLinkAdd = new HtmlAjaxCommandLink();

		MethodExpression commandLinkActionAdd = FacesUtil.getFacesContext()
				.getApplication().getExpressionFactory()
				.createMethodExpression(
						FacesUtil.getFacesContext().getELContext(),
						"#{formRecibo.addNewComponent}", void.class,
						new Class[] { ActionEvent.class });

		commandLinkAdd.addActionListener(new MethodExpressionActionListener(
				commandLinkActionAdd));
		
		commandLinkAdd.setReRender("itensReciboPanel");
		
		HtmlGraphicImage imagemAdd = new HtmlGraphicImage();
		imagemAdd.setAlt("Adicionar");
		imagemAdd.setUrl("/img/add.png");
		imagemAdd.setStyle("border:0px;");
		
		commandLinkAdd.getChildren().add(imagemAdd);
		
		regionAdd.getChildren().add(commandLinkAdd);
		
		HtmlAjaxRegion regionDel = new HtmlAjaxRegion();

		HtmlAjaxCommandLink commandLinkDel = new HtmlAjaxCommandLink();

		MethodExpression commandLinkActionDel = FacesUtil.getFacesContext()
				.getApplication().getExpressionFactory()
				.createMethodExpression(
						FacesUtil.getFacesContext().getELContext(),
						"#{formRecibo.addNewComponent}", void.class,
						new Class[] { ActionEvent.class });

		commandLinkAdd.addActionListener(new MethodExpressionActionListener(
				commandLinkActionDel));
		
		commandLinkDel.setReRender("itensReciboPanel");
		
		HtmlGraphicImage imagemDel = new HtmlGraphicImage();
		imagemDel.setAlt("Remover");
		imagemDel.setUrl("/img/del.png");
		imagemDel.setStyle("border:0px;");
		
		commandLinkDel.getChildren().add(imagemDel);
		
		regionDel.getChildren().add(commandLinkDel);

		children.add(selectOne);
		children.add(outPut);
		children.add(inputText);
//		children.add(regionAdd);
//		children.add(regionDel);
	}

	public HtmlPanelGrid getPanelGrid() {
		return panelGridItenRecibo;
	}

	public HtmlPanelGrid getPanelGridItenRecibo() {
		return panelGridItenRecibo;
	}

	public void setPanelGridItenRecibo(HtmlPanelGrid panelGridItenRecibo) {
		this.panelGridItenRecibo = panelGridItenRecibo;
	}
}
